/**
* @Title: caseAuditList_Ctrl.js
* @Description: TODO
* Copyright: Copyright (c) 2014 
 * Company:中国平安
*
* @Project HIMS-FWA
* @date 2014年10月27日 下午4:43:38
* @version V1.0
*/
;
(function(){
	'use strict';
	
	var app = angular.module('fwa.mainApp');
	app.controller('ProblemResearchListCtrl', ['$scope','$http','$modal','$state','CaseCommonService','CaseAuditListService','CaseAuditDispathReviewResultService',
	                                           'BaseURL','Modal','FwaTabsetService','AuditDictServices','InsuranceService','CommunityService','CaseAuditDispathReviewDetailResultService','AuditCanceService',
	                                function($scope,$http,$modal,$state,CaseCommonService,caseAuditListService,CaseAuditDispathReviewResultService,
	                                		baseUrl,Modal,fwaTabsetService,AuditDictServices,InsuranceService,CommunityService,CaseAuditDispathReviewDetailResultService,AuditCanceService){

		
		$scope.addOnClick = function(){
			fwaTabsetService.selectTab('caseAudit.caseAuditAdd',null,false);
		}
		$scope.communityService = CommunityService;
        CommunityService.initCodeListDatas();
      
		InsuranceService.initMedicalOrgInfoList().success(function(response){
            $scope.medicalOrgInfoList = response.medicalOrgList;
		});
       
		$scope.auditDictServices = AuditDictServices;
		AuditDictServices.initCodeListDatas();

        var cellTemplate = '<a ng-show="data.market ==0" ng-click="queryByProblemCode(data);">查看</a>' +
            '<a ng-show="data.market ==1" ng-click="operation(data);">调查</a>' +
            '<a ng-show="data.market == 2" ng-click="operationDispathch(data);">复核</a>'+
            '<a ng-show="data.market ==3" ng-click="problemCance(data);">撤回</a>' ;
		$scope.columns = [
  		{
            label : '事件号',
            map : 'problemNo',
            width : '10%',
            cellTemplate:'<a  ng:bind="data.problemNo" '+
           'ng-click="queryByProblemCode(data)"></a>'},
        {
			label : '事件时间段',
            
			map : 'periodDate',
			width : '15%'
		}, {
			label : '事件要点',
			map : 'keyPoint',
			width : '20%'
		}, {
			label : '来源方式',
			map : 'sourceTypeDesc',
			width : '10%'
		}, {
			label : '提交日期',
			map : 'submitDate',
			width : '15%'
		}, {
			label : '关联案件号',
			map : 'otherCaseCode',
			width : '10%'
		}, {
			label : '状态',
			map : 'statusName',
			width : '10%'
		}, {
			label : '操作',
			map : '1',
			width : '10%',
			cellTemplate:cellTemplate}
        ];
		
		$scope.operation = function(data){
			if(data.market == 1 && data.rollbackFlag == 1 ){
                $state.go('problemResearch.auditBackDispatchAddResult',{problemNo:data.problemNo,urlFlag:''});
			}else
            {
                $state.go('problemResearch.auditDispatchResult',{problemNo:data.problemNo,urlFlag:''});
            }
		}
        $scope.operationDispathch = function(data){
            if(data.market == 2 && data.rollbackFlag == 1 ) {
                $state.go('problemResearch.auditBackDispatchReviewResult',{problemNo:data.problemNo,urlFlag:''});
            }else
            {
                $state.go('problemResearch.auditDispatchReviewResult',{problemNo:data.problemNo,urlFlag:''});
            }
        }
        
        $scope.queryByProblemCode = function(data){
            $state.go("problemDetailView.detail",{problemCode:data.problemNo,problemStatus:data.problemStatus});
        }
        
        /**
         * 撤回
         */
        $scope.problemCance = function(data) {
        	$state.go("problemResearch.auditCancel",{problemNo:data.problemNo,problemStatus:data.problemStatus});
        }

        /**
         * 稽核
         * @param data
         */
        $scope.projectAudit = function(data){
            fwaTabsetService.selectTab('caseAuditProject',{problemNo:data},false);
        }
        /**
         * 调查
         * @param data
         */
		$scope.dealPorblemNo = function(data){
			fwaTabsetService.selectTab('auditDispatchResult',{problemNo:data},false);
		}
        /**
         * 复核
         * @param data
         */
        $scope.reviewProblemNo = function(data){
            fwaTabsetService.selectTab('auditDispatchReviewResult',{problemNo:data},false);
        }
        /**
         * 查看复核详情
         * @param data
         */
        $scope.reviewProblemNoDetail = function(data){
            fwaTabsetService.selectTab('auditDispatchReviewDetailResult',{problemNo:data},false);
        }
        /**
         * 查看调查详情
         * @param data
         */
        $scope.dealPorblemNoDetail = function(data){
            fwaTabsetService.selectTab('auditDispatchDetailResult',{problemNo:data},false);
        }
		
		$scope.onReset = function(){
            $scope.params.problemCode=null;
            $scope.params.orgName=null;
            $scope.params.hearDate={start:null,end:null};
            $scope.params.sourceType=null;
            $scope.params.doctorName=null;
            $scope.params.jobLicense=null;
            $scope.params.sbmitDate={start:null,end:null};
	        };


            $scope.currentPage = 1;
            var start = 0;
            $scope.limit = 20;
            $scope.params = {
                page:0,
                pageSize:20
            };
            function query () {
            	$scope.params.page =  $scope.currentPage;
                var fparam = $scope.params;
                if($scope.params.hearDate){
                    var hearDateStart = $scope.params.hearDate.start;
                    var hearDateEnd = $scope.params.hearDate.end;
                    fparam.hearDateStart = hearDateStart;
                    fparam.hearDateEnd = hearDateEnd;
                }
                if($scope.params.sbmitDate){
                    var start = $scope.params.sbmitDate.start;
                    var end = $scope.params.sbmitDate.end;
                    fparam.start = start;
                    fparam.end = end;
                }
    	    	fparam.problemStatus ="4,5,6";
                CaseCommonService.loadProblemAuditTask(fparam).success(function(response){
    	    		if(response) {
                		$scope.tableData = response.result;
            			$scope.totalCount = response.totals.total;
    	    		}
    	    	});
            }

            //分页监听
            $scope.$on('pageTool.skipPage',function(e,v){
                $scope.currentPage = v;
                query();
            });


         /**
	     * 
	     **/
	    $scope.onSubmit = function(){
	    	 $scope.currentPage = 1;
             query();
	    }
	    

	    var checkRows = [];
        function findCheck(d) {
            for(var i=0;i< checkRows .length;i++) {
                var checkedData =  checkRows [i];
                if(checkedData.problemNo == d.problemNo) {
                    return i;
                }
            }
            return -1;
        }

       $scope.$on('fwatable.checked', function(e,d){
            if(findCheck(d) == -1) {
                checkRows .push(d);
            }
        });

        $scope.$on('fwatable.unchecked', function(e,d){
            var index = findCheck(d);
            if(index != -1) {
               checkRows.splice(index,1);
            }
        });

        //---------------开始-----------------
        //批量操作（调查）
        $scope.loading = false;
        $scope.onBatchOperateDc = function(){
        	
        	var problemNos= [];
        	if(checkRows.length <= 0){
                Modal.alert('请选择需要操作的选项！');
                return;
	        }else{
	        	//验证勾选项是否全为调查
	        	for ( var i = 0; i < checkRows.length; i++) {
	        		if(checkRows[i].market!=1){
	        			Modal.alert('只能选择待调查状态进行批量操作');
	                    return;
	        		}
	        		problemNos.push(checkRows[i].problemNo);
				}
	        	$scope.loading = true;
        		var parameter={problemNos:problemNos};
        		CaseAuditDispathReviewResultService.addBatchSurResult(parameter).success(function(rst){
                    if(rst.state){
                        Modal.alert(rst.msg);
                        $scope.onSubmit();
                    }
                    $scope.loading = false;
                });
	            }
        }
        

        //---------------结束-----------------

        //批量操作（复核）
	    $scope.onBatchOperate = function(){
	    	$scope.problemNos= [];
            if(checkRows.length <= 0){
                Modal.alert('请选择需要操作的选项！');
                return;
            }else{
            	
	        	//验证勾选项是否全为调查
	        	for ( var i = 0; i < checkRows.length; i++) {
	        		if(checkRows[i].market!=2){
	        			Modal.alert('只能选择复核状态进行批量操作!');
	                    return;
	        		}
	        		$scope.problemNos.push(checkRows[i].problemNo);
				}
	        	var scope = $scope.$new();
                var modalInstance = $modal.open({
                    templateUrl: baseUrl+'views/hcpms/audit/problemResearch/onBatchOperate.html',
                    backdrop:'static',
                    size:'sm',
                	scope:scope,
                    controller: function($scope, $http,$modalInstance,Modal){
                    	$scope.surveyDatail = {};
                        //关闭model
                        $scope.onClose = function() {
                            $modalInstance.close();
                        }
                        $scope.confirm = function(){
                        	var opinionDesc = $scope.surveyDatail.opinionDesc;
                        	var opinionState = $scope.surveyDatail.opinionState;
                        	if(!opinionState){
                        		Modal.alert("复核意见为必填项！");
                        		return ;
                        	}
                        	if(!opinionDesc){
                        		Modal.alert("意见说明为必填项！");
                        		return ;
                        	}
                        	$scope.loading = true;
                    		var parameter={problemNos:$scope.problemNos,
                    				opinionDesc:opinionDesc,
                    				opinionState:opinionState};
                    		CaseAuditDispathReviewResultService.onBatchOperateReview(parameter).success(function(rst){
                                if(rst.state){
                                    Modal.alert(rst.msg);
                                    $scope.onClose();
                                    $scope.onSubmit();
                                }
                                $scope.loading = false;
                        	});
                        }
                    }
                });
            }
        }

	    $scope.onSubmit();
	// end line	
	}]);
})();